﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace D3crft.Windows.UserControls {
    /// <summary>
    /// Interaktionslogik für ucCheckBox.xaml
    /// </summary>
    public partial class ucCheckBox : UserControl {

        public event EventHandler evCheckedChanged;

        private string sText;
        private bool bChecked;
        private bool bAllowUserUncheck;
        Image oImage;
        Label oLabel;

        public ucCheckBox() {
            do_Initialize();
            do_Preset();
            do_Startup();
        }

        private void do_Initialize() {
            InitializeComponent();
        }

        private void do_Preset() {
            sText = "";
            bChecked = false;
            bAllowUserUncheck = true;
            oImage = this.FindName("Image_Box") as Image;
            oLabel = this.FindName("Label_Text") as Label;

            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);
            oImage.MouseEnter += new MouseEventHandler(ev_oImage_MouseEnter);
            oImage.MouseLeave += new MouseEventHandler(ev_oImage_MouseLeave);
            oImage.MouseUp += new MouseButtonEventHandler(ev_oImage_MouseUp);
            oLabel.MouseEnter += new MouseEventHandler(ev_oLabel_MouseEnter);
            oLabel.MouseLeave += new MouseEventHandler(ev_oLabel_MouseLeave);
            oLabel.MouseUp += new MouseButtonEventHandler(ev_oLabel_MouseUp);
        }

        private void do_Startup() {

        }

        private void do_PostStartup() {
            oLabel.Content = sText;
            do_RefreshCheckedImage();
        }

        private void do_SwitchChecked() {
            // todo: graphical...
            if (bChecked) {
                bChecked = false;
            }
            else {
                bChecked = true;
            }
            do_RefreshCheckedImage();
            if (evCheckedChanged != null) evCheckedChanged(this, null);
        }

        private void do_RefreshCheckedImage() {
            if (bChecked) {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_checked.png", UriKind.Absolute));
            }
            else {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_unchecked.png", UriKind.Absolute));
            }
        }



        private void ev_Object_Loaded(object sender, RoutedEventArgs ea) {
            do_PostStartup();
        }

        private void ev_oLabel_MouseEnter(object sender, EventArgs ea) {
            if (bChecked) {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_highlighted_checked.png", UriKind.Absolute));
            }
            else {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_highlighted_unchecked.png", UriKind.Absolute));
            }
        }

        private void ev_oLabel_MouseLeave(object sender, EventArgs ea) {
            do_RefreshCheckedImage();
        }

        private void ev_oLabel_MouseUp(object sender, EventArgs ea) {
            if ((!bChecked) || (bAllowUserUncheck && bChecked)) {
                do_SwitchChecked();
            }
        }

        private void ev_oImage_MouseEnter(object sender, EventArgs ea) {
            if (bChecked) {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_highlighted_checked.png", UriKind.Absolute));
            }
            else {
                oImage.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_checkbox_highlighted_unchecked.png", UriKind.Absolute));
            }
        }

        private void ev_oImage_MouseLeave(object sender, EventArgs ea) {
            do_RefreshCheckedImage();
        }

        private void ev_oImage_MouseUp(object sender, EventArgs ea) {
            if ((!bChecked) || (bAllowUserUncheck && bChecked)) {
                do_SwitchChecked();
            }
        }




        public string Text {
            get { return sText; }
            set { sText = value; if (oLabel != null) oLabel.Content = value; }
        }

        public bool Checked {
            get { return bChecked; }
            set { bChecked = value; do_RefreshCheckedImage(); if (evCheckedChanged != null) evCheckedChanged(this, null); }
        }

        public bool AllowUserUncheck {
            get { return bAllowUserUncheck; }
            set { bAllowUserUncheck = value; }
        }

        public void RevertChecked() {
            if (bChecked) {
                bChecked = false;
                do_RefreshCheckedImage();
            }
        }
    }
}
